package cn.tfyhyc.springcloud.service;

import cn.hutool.core.util.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.concurrent.TimeUnit;

/**
 * @author 头发又黑又长
 * @Date 2022/10/11 16:59
 * @email zwb15083976291@163.com
 */
@Service
public class PaymentService {


    // ======================== 服务降级

    /**
     * 正常访问的服务
     *
     * @param id
     * @return
     */
    public String paymentInfo_OK(Integer id) {
        return "线程池" + Thread.currentThread().getName() + " paymentInfo_OK, id" + id + "\t" + "O(∩_∩)O haha~~";
    }

    /**
     * 超时的服务
     * <p>
     * <p>
     * 该接口设置服务降级
     * fallbackMethod
     * 服务降级执行的方法
     * commandProperties
     * "execution.isolation.thread.timeoutInMilliseconds" :指定的超时时间
     *
     * @param id
     * @return
     */
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",
            commandProperties = {
                    @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS,
                            value = "5000")})
    public String paymentInfo_TimeOut(Integer id) {
        int timeNumber = 3;
        // 暂停几秒线程
//        int page = timeNumber / 0; // by zero 异常
        try {
            TimeUnit.SECONDS.sleep(timeNumber);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "线程池" + Thread.currentThread().getName() + " paymentInfo_TimeOut, id" + id + "\t" + "O(∩_∩)O haha~~" + "  耗时" + timeNumber + "秒";
    }


    // 服务降级执行的方法
    public String paymentInfo_TimeOutHandler(@PathVariable("id") Integer id) {
        return "线程池" + Thread.currentThread().getName() + " paymentInfo_TimeOutHandler 服务器繁忙，请稍后再尝试, id: " + id + "\t" + "    /(ㄒoㄒ)/ ~~ haha~~";
    }

    //==========================服务熔断
    @HystrixCommand(
            fallbackMethod = "paymentCircuitBreaker_fallback",
            commandProperties = {
                    // 是否开启短路器
                    @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ENABLED, value = "true"),
                    // 请求次数
                    @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD, value = "10"),
                    //熔断时间窗口期
                    @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS, value = "10000"),
                    // 失败率达到多少后跳闸
                    @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE, value = "60")})
    public String paymentCircuitBreaker(Integer id) {
        if (id < 0) {
            throw new RuntimeException("************** id 不能为复数");
        }
        String serialNumber = IdUtil.simpleUUID();
        return Thread.currentThread().getName() + "\t 调用成功,流水号：" + serialNumber;
    }


    public String paymentCircuitBreaker_fallback(Integer id) {
        return "id 不能为负数，请稍后再试，/(ㄒoㄒ)/~~     id:" + id;
    }


}
